package com.competition.front.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.competition.commonutils.JwtUtils;
import com.competition.commonutils.R;
import com.competition.front.entity.ExaminationDetail;
import com.competition.front.entity.UserCompetition;
import com.competition.front.entity.vo.ScoreVo;
import com.competition.front.service.ExaminationDetailService;
import com.competition.front.service.UserCompetitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 知识竞赛题目表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/front/examination-detail")
@CrossOrigin
@Api(description = "试题相关接口")
public class ExaminationDetailController {


    @Autowired
    private ExaminationDetailService examinationDetailService;

    @Autowired
    private UserCompetitionService userCompetitionService;

    @GetMapping("getExaminationSubject/{examinationId}")
    @ApiOperation("根据竞赛id获取竞赛题目")
    private R getExaminationSubject(@PathVariable String examinationId){
        List<ExaminationDetail> examinationDetails=examinationDetailService.getExaminationSubject(examinationId);
        return R.ok().data("examinationDetails",examinationDetails);
    }

    @PostMapping("checkAnswer")
    @ApiOperation("接口描述：传递竞赛题目对应的id（examinationId为竞赛的id）和答案（数组）(userAnswer: {}的对象为题目id和用户答案)  不确定接口无bug,有bug直接联系我")
    public R statisticalScore(@RequestBody ScoreVo scoreVo, HttpServletRequest request){
        //获取用户id
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        String examinationId = scoreVo.getExaminationId();
        QueryWrapper<UserCompetition> userCompetitionQueryWrapper=new QueryWrapper<>();
        userCompetitionQueryWrapper.eq("user_id",userId);
        userCompetitionQueryWrapper.eq("examination_id",examinationId);
        List<UserCompetition> userCompetitionList = userCompetitionService.list(userCompetitionQueryWrapper);
        if(userCompetitionList.size()>=1){
            return R.error().data("msg","亲，您已经完成该竞赛");
        }
        R userMap=R.ok();
        return examinationDetailService.statisticalScore(scoreVo,userId,userMap);
    }




}

